package com.IR.order.service;

import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.IR.shop.mapper.CarsMapper;
import com.IR.shop.mapper.NormsMapper;
import com.IR.shop.mapper.OrderdetailMapper;
import com.IR.shop.mapper.OrdersMapper;
import com.IR.shop.mapper.ProductMapper;
import com.IR.shop.mapper.SiteMapper;
import com.IR.shop.po.Norms;
import com.IR.shop.po.Orderdetail;
import com.IR.shop.po.OrderdetailExample;
import com.IR.shop.po.Orders;
import com.IR.shop.po.Product;
import com.IR.shop.po.Saveorder;
import com.IR.shop.po.Site;

@Service
public class AOrderBiz {
	@Resource(name="ordersmapper")
	public OrdersMapper mapper1;
	@Resource(name="orderdetailmapper")
	public OrderdetailMapper mapper2;
	@Resource(name="productmapper")
	public ProductMapper mapper3;
	@Resource(name="carsmapper")
	public CarsMapper mapper4; 
	@Resource
	public NormsMapper mapper5;
	@Resource
	public SiteMapper mapper6;
	private Orders orders;
	private Orderdetail orderdetail;
	private List<Orders> list1;
	private List<Orderdetail> list2;
	public Orders getOrders() {
		return orders;
	}
	public void setOrders(Orders orders) {
		this.orders = orders;
	}
	public Orderdetail getOrderdetail() {
		return orderdetail;
	}
	public void setOrderdetail(Orderdetail orderdetail) {
		this.orderdetail = orderdetail;
	}
	public List<Orders> getList1() {
		return list1;
	}
	public void setList1(List<Orders> list1) {
		this.list1 = list1;
	}
	public List<Orderdetail> getList2() {
		return list2;
	}
	public void setList2(List<Orderdetail> list2) {
		this.list2 = list2;
	}
	
	//立即购买
	public void saveOrder1(Saveorder save,int siteid) throws ParseException {
		Orders o=new Orders();
		o.setOrderstate(1);
		o.setPayway(1);
		o.setSiteid(siteid);
		o.setUserid(save.getUserid());
		Site s=new Site();
		s=mapper6.selectByPrimaryKey(siteid);
		o.setSphone(s.getSphone());
		o.setSsite(s.getSsite());
		o.setSuser(s.getSuser());
		Date date = new Date();	
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String sss=format.format(date);		
		SimpleDateFormat format1 = new SimpleDateFormat("yyyyMMddHHmmssSSS");
		String ss=format1.format(date);
		String ssss=ss.substring(0,16);
		o.setOrderid(new Long(ssss));
		o.setOrderdate(format.parse(sss));
		Orderdetail or=new Orderdetail();
		or.setOrderid(new Long(ss));
		or.setProid(save.getProid());
		or.setPronumber(save.getPronumber());
		or.setNormsid(save.getNormsid());
		or.setNormsdetail(save.getNormsdetail());
 		or.setProimgs(save.getProimgs());
 		or.setProname(save.getProname());
 		or.setProprice(save.getProprice());
 		Double d=save.getProprice()*(save.getPronumber());
 		mapper2.insert(or);						
		DecimalFormat df = new DecimalFormat("#.00");  
        String dd=df.format(d); 
        Double d2=Double.valueOf(dd.toString());
		System.out.println(d2);
		o.setOrderprice(d2);
		mapper1.insert(o);
	}
	//购买
	public void saveOrder2(List<Saveorder> list,int siteid) throws ParseException {
		Orders o=new Orders();
		o.setOrderstate(1);
		o.setPayway(1);
		o.setSiteid(siteid);
		o.setUserid(list.get(0).getUserid());
		Site s=new Site();
		s=mapper6.selectByPrimaryKey(siteid);
		o.setSphone(s.getSphone());
		o.setSsite(s.getSsite());
		o.setSuser(s.getSuser());
		Date date = new Date();	
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String sss=format.format(date);		
		SimpleDateFormat format1 = new SimpleDateFormat("yyyyMMddHHmmssSSS");
		String ss=format1.format(date);
		String ssss=ss.substring(0,16);
		o.setOrderid(new Long(ssss));
		o.setOrderdate(format.parse(sss));
		List<Double> list1=new ArrayList<Double>();
		for(Saveorder save:list) {
			Orderdetail or=new Orderdetail();
			or.setOrderid(new Long(ssss));
			or.setProid(save.getProid());
			or.setPronumber(save.getPronumber());
			or.setNormsid(save.getNormsid());
			/*Norms n=new Norms();
			n=mapper5.selectByPrimaryKey(save.getNormsid());*/
			or.setNormsdetail(save.getNormsdetail());
			/*Product p=new Product();
	 		p=mapper3.selectByPrimaryKey(save.getProid());*/
	 		or.setProimgs(save.getProimgs());
	 		or.setProname(save.getProname());
	 		or.setProprice(save.getProprice());
	 		Double d=save.getProprice()*(save.getPronumber());
	 		list1.add(d);
	 		mapper2.insert(or);	
		}
		Double d1=0.00;
		for(int i=0;i<list1.size();i++) {		
			d1=d1+list1.get(i);
		}
		DecimalFormat df = new DecimalFormat("#.00");  
        String dd=df.format(d1); 
        Double d2=Double.valueOf(dd.toString());
		System.out.println(d2);
		o.setOrderprice(d2);
		mapper1.insert(o);
	}
	public void deleteOrderDetail() {
		mapper2.deleteByPrimaryKey(35);
	}
	public void deleteOrder() {
		OrderdetailExample example=new OrderdetailExample();
		OrderdetailExample.Criteria c=example.createCriteria();
		c.andOrderidEqualTo(20180906141828828l);
		mapper2.deleteByExample(example);
		mapper1.deleteByPrimaryKey(20180906141828828l);
	}
	public void delete() {
		OrderdetailExample example=new OrderdetailExample();
		OrderdetailExample.Criteria c=example.createCriteria();
		//c.and
	}
	public void updateOrderState(Long orderid,int orderstate) {
		orders=mapper1.selectByPrimaryKey(orderid);
		orders.setOrderstate(orderstate+1);
		mapper1.updateByPrimaryKey(orders);
	}
	//前台添加回收站
	public void updateOrderdelete(Long orderid) {
		orders=mapper1.selectByPrimaryKey(orderid);
		orders.setOrderstate(7);
		mapper1.updateByPrimaryKey(orders);
	}
	public void updateOrdertuihuo(Long orderid) {
		orders=mapper1.selectByPrimaryKey(orderid);
		orders.setOrderstate(5);
		mapper1.updateByPrimaryKey(orders);
	}
	//同意退货
	public void updateOrdertuihuo2(Long orderid) {
		orders=mapper1.selectByPrimaryKey(orderid);
		orders.setOrderstate(6);
		mapper1.updateByPrimaryKey(orders);	
	}
	public void updateOrderfahuo(Long orderid) {
		orders=mapper1.selectByPrimaryKey(orderid);
		orders.setOrderstate(3);
		mapper1.updateByPrimaryKey(orders);	
	}
	public static void main(String[] args) {
		int a=1;
		int b=2;
		System.out.println(a+b);
	}
}
